package com.into.common.csv.converters;

import com.into.common.csv.CSVConverter;

public class EnumConverter<T extends Enum<T>> implements CSVConverter<T> {
	protected T[]							enums;
	protected EnumLabeller<T>	labeller;

	public EnumConverter(T[] enums, EnumLabeller<T> labeller) {
		this.enums = enums;
		this.labeller = labeller;
	}

	@Override
	public T fromString(String value) throws Exception {
		for (T current : enums) {
			String label = labeller.getLabel(current);
			if (label == value || label.equals(value))
				return current;
		}
		throw new IllegalArgumentException(value);
	}

	@Override
	public String toString(T value) throws Exception {
		return labeller.getLabel(value);
	}

}
